//
// Created by JZYFC on 2021/11/29.
//
#include "menu.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <signal.h>
#include "../console/ConsoleSettings.h"
#include "../gui/WndUtil.h"
#include "../inout/ddlinout.h"
#include "../note_by_ps/addnew.h"
#include "../note_by_WDC/search.h"
#include "../zm/del_and_mod.h"
#include "../note_by_ps/printall.h"

void show_main_menu_message();

void TMP_show_plan(list_head week_head, list_head day_head);

static int exit_signal = 1;
bool WINAPI CtrlHandler(DWORD ctrlType) {
    switch (ctrlType) {
        case CTRL_CLOSE_EVENT:
        case CTRL_BREAK_EVENT:
        case CTRL_C_EVENT:
            exit_signal = 0;
            exit(-1);
            break;
    }
    return false;
}

void signalHandler(int signum) {
    exit_signal = 0;
    exit(-2);
}

void main_menu(void) {
    // 初始化

    list_head week_head = (list_head) calloc(1, sizeof(linked_list));
    list_head day_head = (list_head) calloc(1, sizeof(linked_list));

    list_head current;

    // TODO: DELETE TEST CODE

    day_data testData = {
            0,
            {2021, 11, 11, 11, 11},
            {2021, 11, 11, 11, 13},
            L"1\r\n"
    };

    // WEEK TEST
    current = week_head;
    for (int i = 0; i < 3; ++i) {
        linked_list *node = (list_head) calloc(1, sizeof(linked_list));
        testData.number = i;
        swprintf(testData.todo, 2, L"%d", i);
        node->data = testData;
        current->next = node;
        current = current->next;
    }

    // DAY TEST
    current = day_head;
    for (int i = 0; i < 3; ++i) {
        linked_list *node = (list_head) calloc(1, sizeof(linked_list));
        testData.number = i;
        swprintf(testData.todo, 2, L"%d", i+3);
        node->data = testData;
        current->next = node;
        current = current->next;
    }

    // TODO: init_list()

    int input = -10;
    setlocale(LC_ALL, "");
    signal(SIGINT, signalHandler);
    signal(SIGBREAK, signalHandler);

    update_stdout_handle(GetStdHandle(STD_OUTPUT_HANDLE));
    SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, true);

    list_head head_tmp;

    while (exit_signal) {
        TMP_show_plan(week_head, day_head);
        show_main_menu_message();
        if (wscanf_s(L"%d", &input) == 0) goto input_err;
        switch (input) {
            case 1:
                wprintf(L"请输入要更改的计划类型编号（周计划为1，日计划为2）\n");
                wscanf_s(L"%d", &input);
                day_data dd;
                if (input == 1) {
                    dd = read_info(week_head, true);
                    addnew(week_head, dd);
                } else if (input == 2) {
                    dd = read_info(day_head, true);
                    addnew(day_head, dd);
                } else goto input_err;
                break;
            case 2:
                wprintf(L"请输入要更改的计划类型编号（周计划为1，日计划为2）\n");
                wscanf_s(L"%d", &input);
                day_data data;
                if (input == 1) {
                    data = read_info(week_head, false);
                    modifyNode(week_head, data);
                } else if (input == 2) {
                    data = read_info(day_head, false);
                    modifyNode(day_head, data);
                } else goto input_err;
                break;
            case 3:
                wprintf(L"请输入要查找的计划类型编号（周计划为1，日计划为2）\n");
                wscanf_s(L"%d", &input);
                if (input == 1) {
                    head_tmp = week_head;
                } else if (input == 2) {
                    head_tmp = day_head;
                } else goto input_err;
                wprintf(L"请输入要删除的计划的编号\n");
                wscanf_s(L"%d", &input);
                day_data mData = {input, {0,0,0,0,0},
                                  {0,0,0,0,0},
                                  L""};
                deleteNode(head_tmp, mData);
                break;
            case 4:
                wprintf(L"请输入要查找的计划类型编号（周计划为1，日计划为2）\n");
                wscanf_s(L"%d", &input);
                if (input == 1) {
                    head_tmp = week_head;
                } else if (input == 2) {
                    head_tmp = day_head;
                } else goto input_err;
                wprintf(L"请输入查找的数据：（查找计划开始日期为1，查找计划结束日期为2,忽略小时与分钟）\n");
                wscanf_s(L"%d", &input);
                if (input != 1 && input != 2) goto input_err;
                wprintf(L"请输入日期: \n");
                date d = read_date();
                if (input == 1) {
                    search_start(week_head, d);
                } else if (input == 2) {
                    search_end(day_head, d);
                }
                break;
            case 5:
                FILE *file = NULL;
                file = fopen("note.exe", "r");
                if (file == NULL)
                    wprintf(L"笔记本模块不存在");
                else system("start cmd /k note.exe");
                break;
            case 6:
                wprintf(L"正在图形界面中操作。");
                create_and_show_main_window(GetConsoleWindow(), week_head, day_head);
                break;
            case 0:
                // TODO: 保存并退出
                break;
            case -1:
                // TODO: 退出但不保存
                // TODO: Memory clear
                exit(0);
                break;
            default:
                goto input_err;
                break;
        }
        input = -10;
        clear_console();
        continue;

        input_err:
        set_console_text_color(RED);
        clear_console();
        wprintf(L"错误编号，请重新输入\n");
        restore_prev_color();
        clear_input_stream();
    }
}

void show_main_menu_message() {
    wprintf(L"请输入你的选项：\n");
    wprintf(L"1. 添加计划\n");
    wprintf(L"2. 修改计划\n");
    wprintf(L"3. 删除计划\n");
    wprintf(L"4. 查找计划\n");
    wprintf(L"5. 打开笔记本\n");
    set_console_text_color(BLUE);
    wprintf(L"6. 使用图形界面操作\n");
    restore_prev_color();
    wprintf(L"0. 保存并退出\n");
    set_console_text_color(RED);
    wprintf(L"-1. 退出但不保存\n");
    restore_prev_color();
    wprintf(L"> ");
}

void TMP_show_plan(list_head week_head, list_head day_head) {
    set_console_text_color(BLUE);
    wprintf(L"======周计划======\n");
    restore_prev_color();
    printall(week_head);
    set_console_text_color(BLUE);
    wprintf(L"==================\n");
    restore_prev_color();
    set_console_text_color(BLUE);
    wprintf(L"======日计划======\n");
    restore_prev_color();
    printall(day_head);
    set_console_text_color(BLUE);
    wprintf(L"==================\n");
    restore_prev_color();

}
